package com.bush.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.ware.mapper.WareInfoMapper;
import com.bush.mall.ware.entity.WareInfoEntity;
import com.bush.mall.ware.service.WareInfoService;

/**
 * 仓库信息
 * @author RUN
 * @date 2022-11-29
 */
@Service(WareInfoService.BEAN_ID)
@Slf4j
public class WareInfoServiceImpl extends ServiceImpl<WareInfoMapper, WareInfoEntity> implements WareInfoService {

    /**
     * 查询仓库信息列表
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<WareInfoEntity> page = new Page<>(current,size);
            QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("WareInfoService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建仓库信息表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result saveWareInfo(WareInfoEntity wareInfo) {
        try{
            Assert.notNull(wareInfo,"参数异常");
            boolean flag = this.save(wareInfo);
            if (flag) return Result.ok("仓库信息添加成功");
            throw new SaaSException("仓库信息添加失败");
        }catch(Exception e){
            log.error("WareInfoService saveWareInfo ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改仓库信息表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result updateWareInfo(WareInfoEntity wareInfo) {
        try{
            Assert.notNull(wareInfo,"参数异常");
            boolean flag = this.updateById(wareInfo);
            if (flag) return Result.ok("仓库信息修改成功");
            throw new SaaSException("仓库信息修改失败");
        }catch(Exception e){
            log.error("WareInfoService updateWareInfo ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除仓库信息表单
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result deleteWareInfo(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("仓库信息删除成功");
            throw new SaaSException("仓库信息删除失败");
        }catch(Exception e){
            log.error("WareInfoService deleteWareInfo ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}